﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Roguelike
{
    public class TextMapRenderer : IMapRenderer
    {
        // Stores the locations of all entity and tile textGraphics
        // This is a jagged array instead of a two-dimensional array to make printing easier
        private char[][] graphicsMap;

        int mapX;
        int mapY;

        /// <summary>
        /// Constructor for TextMapRenderer
        /// </summary>
        public TextMapRenderer(int mapX_, int mapY_)
        {
            mapX = mapX_;
            mapY = mapY_;
        }

        /// <summary>
        /// Initializes graphicsMap
        /// </summary>
        public void GenerateGraphicsMap()
        {
            graphicsMap = new char[mapY][];

            // Creates a jagged array copy of the map that has the tiles' corresponding textGraphics instead of the tile IDs
            for (int y = 0; y < mapY; y++)
            {
                graphicsMap[y] = new char[mapX];

                for (int x = 0; x < mapX; x++)
                {
                    // Gets the tile's graphic from the TileManager, which gets the tile from the ScreenManager, and assigns the textGraphic
                    graphicsMap[y][x] = Singleton<TileManager>.instance.GetTextGraphic(x, y);
                }
            }

            // Replaces the tile textGraphic with the entity textGraphic everywhere there's an entity
            foreach (Entity entity in Singleton<GameManager>.instance.entities)
            {
                graphicsMap[entity.y][entity.x] = entity.GetTextGraphic();
            }
        }

        /// <summary>
        /// Takes information about an entity moving and updates the graphics map
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="oldX"></param>
        /// <param name="oldY"></param>
        /// <param name="newX"></param>
        /// <param name="newY"></param>
        public void UpdateGraphicsMap(Entity entity, int oldX, int oldY, int newX, int newY, bool entityExists = true)
        {
            // Replace entity textGraphic at old coordinates with the tile textGraphic for those coordinates
            graphicsMap[oldY][oldX] = Singleton<TileManager>.instance.GetTextGraphic(oldX, oldY);

            // If the entity still exists, replace the tile textGraphic at the new coordinates with the entity textGraphic
            if (entityExists == true)
            {
                graphicsMap[newY][newX] = entity.GetTextGraphic();
            }
        }

        /// <summary>
        /// Prints the map to the console
        /// </summary>
        public void DrawMap()
        {
            // Clears the console so the new map can be printed
            Console.Clear();

            // Converts each line to a string and prints it out
            for (int y = 0; y < mapY; y++)
            {
                Console.WriteLine(new string(graphicsMap[y]));
            }
        }
    }
}
